<template>
  <el-drawer
    :append-to-body="true"
    :wrapperClosable="false"
    title="错题记录"
    size="840px"
    :visible="open"
    @close="handleClose"
  >
    <div class="drawer_container">
      <el-table
        v-loading="loading"
        :data="answerRecordList"
        ref="multipleTable"
        height="calc(100vh - 190px)"
        :header-cell-style="tableHeaderStyle"
        class="cum_table"
        row-key="id"
      >
        <el-table-column type="index" align="center"></el-table-column>
        <el-table-column label="知识点" align="center">
          <template slot-scope="scope">
            <ancestorsNameDeal
              :text="scope.row.indexName"
              :maxLength="36"
            >
            </ancestorsNameDeal>
          </template>
        </el-table-column>
        <el-table-column label="试题来源" width="80" align="center">
          <template slot-scope="scope">
            {{ paperTypeText[scope.row.source] }}
          </template>
        </el-table-column>
        <el-table-column label="答错时间" width="160" align="center">
          <template slot-scope="scope">
            <div>{{ parseTime(scope.row.wrongTime) }}</div>
          </template>
        </el-table-column>
      </el-table>
      <div class="btns">
        <el-button @click="handleClose">关闭</el-button>
      </div>
    </div>
  </el-drawer>
</template>

<script>
import { getMistakeTopicRecordApi } from '@/api/ExaminationModule/wrongQuestion'
import ancestorsNameDeal from '@/components/ancestorsNameDeal'

export default {
  name: 'wrongRecordDrawer',
  components: {
    ancestorsNameDeal
  },
  props: {
    open: {
      type: Boolean,
      default: false
    },
    courseId: {
      type: [Number, String],
      default: 0
    },
    topicId: {
      type: [Number, String],
      default: 0
    }
  },
  watch: {
    open: {
      handler(val) {
        if (val) {
          this.getMistakeTopicRecord()
        }
      }
    }
  },
  data() {
    return {
      paperTypeText: {
        1: '考核',
        3: '练习',
        6: '作业'
      },
      tableHeaderStyle: {
        'background-color': '#f8f8f9',
        'color': '#333333',
        'font-size': '14px',
        'font-family': 'PingFangSC, PingFang SC',
        'font-weight': '400'
      },
      loading: false,
      answerRecordList: []
    }
  },
  methods: {
    async getMistakeTopicRecord() {
      this.loading = false
      try {
        let res = await getMistakeTopicRecordApi(this.courseId, this.topicId)
        const { code, data } = res
        if (code === 200) {
          this.answerRecordList = data
        }
      } catch (e) {
        console.error(e)
      } finally {
        this.loading = false
      }
    },
    //关闭抽屉
    handleClose() {
      this.$emit('update:open', false)
      this.answerRecordList = []
    }
  }
}
</script>

<style scoped lang="scss">
.drawer_container {
  height: 100%;
  padding: 17px 31px;
  box-sizing: border-box;
  border-top: 1px solid #EDEFF0;

  .cum_table {
    width: 100%;
    border-radius: 8px;
    border: 1px solid #E8EAEB;
    margin-top: 16px;
  }

  ::v-deep .el-table:before {
    height: 0;
  }

  .btns {
    margin-top: 20px;
    width: 100%;
    display: flex;
    justify-content: flex-end;
  }
}
</style>
